package com.briup.pts.exception;

import com.briup.pts.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 全局异常处理，用来增强Controller返回json格式错误信息的功能
 */
//@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Object handlerException(Exception e){
        // 定义返回的结果，和controller一样
        Result result = null;
        if (e instanceof ServiceException){
            result = Result.failure(((ServiceException) e).getResultCode());
        }else {
            result = Result.failure(500,"服务器未知错误"+e.getMessage());
        }
        return result;
    }
}
